package com.kzj.service.impl;

import com.kzj.domain.Administrator;
import com.kzj.domain.Student;
import com.kzj.domain.Teacher;
import com.kzj.domain.dto.UserLoginDTO;
import com.kzj.domain.vo.UserLoginVO;
import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.exception.MyException;
import com.kzj.global.re.Result;
import com.kzj.global.threadLocal.UserThreadLocal;
import com.kzj.global.utils.CheckCodeUtil;
import com.kzj.global.utils.EncodeUtil;
import com.kzj.global.utils.MatchesUtil;
import com.kzj.service.AdministratorService;
import com.kzj.service.CommonService;
import com.kzj.service.StudentService;
import com.kzj.service.TeacherService;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    AdministratorService administratorService;
    @Override
    public Result login(UserLoginDTO userLoginDTO) {
        Class aClass = MatchesUtil.idType(userLoginDTO.getId());
        String code = stringRedisTemplate.opsForValue().
                get(EncodeUtil.encode(userLoginDTO.getId(), GlobalConstant.USER_CODE_PREFIX));
        if(code==null)
        {
            throw new MyException(GlobalConstant.LOGIN_ERROR);
        }
        userLoginDTO.setCode(userLoginDTO.getCode().toLowerCase());
        if(!code.equals(userLoginDTO.getCode()))
        {
            throw new MyException(GlobalConstant.CODE_ERROR);
        }
        if(userLoginDTO.getPassword()==null)
        {
            throw new MyException(GlobalConstant.PASSWORD_NO_NULL);
        }
        userLoginDTO.setPassword(EncodeUtil.encode(userLoginDTO.getPassword()));
        if(aClass.equals(Student.class))
        {
            return  studentService.login(userLoginDTO);
        }
        else if(aClass.equals(Teacher.class))
        {
            return teacherService.login(userLoginDTO);
        }
        else if(aClass.equals(Administrator.class))
        {
            return administratorService.login(userLoginDTO);
        }
        else return null;
    }

    @Override
    public Result sendImageCode(UserLoginDTO userLoginDTO, HttpServletResponse response) {
        MatchesUtil.idMatches(userLoginDTO.getId());
        String code;
        try {
             code= CheckCodeUtil.outputVerifyImage(120, 40, response.getOutputStream(), 4);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        code = code.toLowerCase();
        System.out.println(code);
        stringRedisTemplate.opsForValue().set(EncodeUtil.encode(userLoginDTO.getId()
                ,GlobalConstant.USER_CODE_PREFIX),code,GlobalConstant.CODE_TTL, TimeUnit.MINUTES);
        return Result.success();
    }

    @Override
    public Result getSelf() {
        return Result.success(UserThreadLocal.getObj());
    }
}
